import sys
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mxs.settings")
django.setup()
from django.db.models import Q
from celery import Celery, platforms
from get_novel import get_novel_base_info as get_novel_binfo
from get_novel import get_chapter_info as get_chapterinfo
from get_novel import string_similar
from demo.models import NovelCategory, Novel, NovelChapter
from celery.schedules import crontab
import requests
import json
import logging
logger = logging.getLogger('tasks')

CELERY_BROKER = "redis://:12345@127.0.0.1:6379/10"
app = Celery('tasks', broker=CELERY_BROKER)
app.conf.timezone = 'Asia/Shanghai'
platforms.C_FORCE_ROOT = True


@app.task
def get_novel_base_info(nurl=""):
    """获取小说基本信息, 获取章节信息"""
    novel_dict = get_novel_binfo(nurl=nurl)
    try:
        # 01 获取小说所属分类
        novel_cat_objs = NovelCategory.objects.all()
        for novel_cat in novel_cat_objs:
            if string_similar(novel_cat.name, novel_dict['novel_category']) >= 0.5:
                novel_dict['novel_category_id'] = novel_cat.id
                break
        # 02 创建/更新小说基本信息
        novel_obj, is_created = Novel.objects.update_or_create(
                                             name=novel_dict['name'],
                                             author=novel_dict['author'],
                                             url=novel_dict['url'],
                                             defaults={"img_url": novel_dict['img_url'],
                                                       "novel_category_id": novel_dict.get('novel_category_id', 8), # 其他
                                                       "publish_state": novel_dict['publish_state'],
                                                       "desc": novel_dict['desc'],
                                                       "update_chapter": novel_dict['update_chapter'],
                                                       "update_chapter_url": novel_dict['update_chapter_url'],
                                                       "publish_date": novel_dict['publish_date'],})
        logger.info("novel_obj: {}".format({"id": novel_obj.id,
                                            "name": novel_obj.name,
                                            "author": novel_obj.author,
                                            "url": novel_obj.url,}))
        # 03 创建或更新章节URL
        chapter_list = novel_dict['chapters']
        for c in chapter_list:
            c_obj, is_created = NovelChapter.objects.update_or_create(
                                                  url=c['url'],
                                                  defaults={"name": c['name'],
                                                            "novel_id": novel_obj.id,
                                                            "sequence": len(chapter_list) - chapter_list.index(c)})
        # 04 调用抓取章节内容的任务
        get_chapter_info.delay()
    except Exception as e:
        logger.error("[get_novel_base_info]except: {}".format(str(e)))


@app.task
def get_chapter_info():
    """抓取小说的章节内容"""
    chapter_objs = NovelChapter.objects.filter(state__in=[1, 3])
    for chapter_obj in chapter_objs:
        try:
            chapter_info = get_chapterinfo(chapter_url=chapter_obj.url)
            if chapter_info['content']:
                chapter_obj.content = chapter_info['content']
                chapter_obj.state = 2
                chapter_obj.save()
            else:
                pass
        except Exception as e:
            chapter_obj.state = 3
            chapter_obj.save()
            logger.error("[get_chapter_info]url:{} except: {}".format(chapter_obj.url, str(e)))




'''
celery -A utils.tasks worker  --loglevel=info -P gevent
'''